home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / gfx / 3d / irit50src.lha / irit5 / amigalib / popen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-05  |  2.9 KB  |  134 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "popen.h"
  4. #include <string.h>
  5.  
  6. #ifndef AMIGA
  7. static char template[] = "piXXXXXX";
  8. #else
  9. static void mkcmd(char *, char *, char, char *);
  10. extern char *mktemp(char *);
  11. #endif
  12.  
  13. typedef enum { unopened = 0, reading, writing } pipemode;
  14. static
  15. struct {
  16.     char *command;
  17.     char *name;
  18.     pipemode pmode;
  19. } pipes[FOPEN_MAX];
  20.  
  21. FILE *
  22. popen( const char *command, const char *mode ) {
  23.     FILE *current;
  24.     char *name;
  25.     int cur;
  26.     pipemode curmode;
  27. #ifdef AMIGA
  28. char template[] = "T:piXXXXXX";
  29. #endif
  30.     /*
  31.     ** decide on mode.
  32.     */
  33.     if(strcmp(mode,"r") == 0)
  34.         curmode = reading;
  35.     else if(strcmp(mode,"w") == 0)
  36.         curmode = writing;
  37.     else
  38.         return NULL;
  39.     /*
  40.     ** get a name to use.
  41.     */
  42. #ifdef AMIGA
  43.     name = mktemp(template);
  44. #else
  45.     if((name = tempnam(".","pip"))==NULL)
  46.         return NULL;
  47. #endif
  48.     /*
  49.     ** If we're reading, just call system to get a file filled with
  50.     ** output.
  51.     */
  52.     if(curmode == reading) {
  53.         char cmd[256];
  54. #ifdef AMIGA
  55.     mkcmd(cmd, command, '>', name);
  56. #else
  57.         sprintf(cmd,"%s > %s",command,name);
  58. #endif
  59.         system(cmd);
  60.         if((current = fopen(name,"r")) == NULL)
  61.             return NULL;
  62.     } else {
  63.         if((current = fopen(name,"w")) == NULL)
  64.             return NULL;
  65.     }
  66.     cur = fileno(current);
  67.     pipes[cur].name = strdup(name);
  68.     pipes[cur].pmode = curmode;
  69.     pipes[cur].command = strdup(command);
  70.     return current;
  71. }
  72.  
  73. int
  74. pclose( FILE * current) {
  75.     int cur = fileno(current),rval;
  76.     /*
  77.     ** check for an open file.
  78.     */
  79.     if(pipes[cur].pmode == unopened)
  80.         return -1;
  81.     if(pipes[cur].pmode == reading) {
  82.         /*
  83.         ** input pipes are just files we're done with.
  84.         */
  85.         rval = fclose(current);
  86.         unlink(pipes[cur].name);
  87.     } else {
  88.         /*
  89.         ** output pipes are temporary files we have
  90.         ** to cram down the throats of programs.
  91.         */
  92.         char command[256];
  93.         fclose(current);
  94. #ifdef AMIGA
  95.     mkcmd(command, pipes[cur].command, '<', pipes[cur].name);
  96. #else
  97.         sprintf(command,"%s < %s",pipes[cur].command,pipes[cur].name);
  98. #endif
  99.         rval = system(command);
  100.         unlink(pipes[cur].name);
  101.     }
  102.     /*
  103.     ** clean up current pipe.
  104.     */
  105.     pipes[cur].pmode = unopened;
  106.     free(pipes[cur].name);
  107.     free(pipes[cur].command);
  108.     return rval;
  109. }
  110. #ifdef AMIGA
  111. /* ^%#@%! Amiga CLI requires I/O redirection immediately after command name.
  112.    Assume command name ends at first space and place redirection there */
  113.  
  114. static void
  115. mkcmd(char *cmd, char *command, char op, char *name)
  116. {
  117.   while (*command == ' ') {
  118.     command++;
  119.   }
  120.   while (*command != ' ' && *command != '\000') {
  121.     *cmd++ = *command++;
  122.   }
  123.   *cmd++ = ' ';
  124.   *cmd++ = op;
  125.   strcpy(cmd, name);
  126.   cmd += strlen(name);
  127.   while (*command != '\000') {
  128.     *cmd++ = *command++;
  129.   }
  130.   *cmd = '\000';
  131.   return;
  132. }
  133. #endif
  134.